package com.kyivmohyla.kma2.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kyivmohyla.kma2.client.LoginService;
import com.kyivmohyla.kma2.client.UserService;
import com.kyivmohyla.kma2.client.events.LoginAllowEvent;
import com.kyivmohyla.kma2.client.events.engineer.EngineerLoginEvent;
import com.kyivmohyla.kma2.client.events.manager.ManagerLoginEvent;
import com.kyivmohyla.kma2.client.events.operator.OperatorLoginEvent;
import com.kyivmohyla.kma2.client.gin.Injector;
import com.kyivmohyla.kma2.client.utils.Dialog;
import com.kyivmohyla.kma2.shared.model.UIStaff;

public class LoginViewPresenter implements Presenter {

    private final Display display;
    private final Injector injector = Injector.INSTANCE;

    public LoginViewPresenter() {
        this.display = injector.getLoginView();
        bind();
    }

    private void bind() {
        this.display.getLoginButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doLogin();
            }
        });
        this.display.getPasswordResetButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doPasswordReset();
            }
        });
    }

    private void doLogin() {
        String login = this.display.getLogin().getText().toLowerCase();
        String password = this.display.getPassword().getText();
        try {
            LoginService.App.getInstance().getByLoginAndPassword(login, password, new AsyncCallback<UIStaff>() {
                @Override
                public void onFailure(Throwable throwable) {
                    Dialog.getInstance().createDialogBoxWithMessage("Error", throwable.getMessage());
                }

                @Override
                public void onSuccess(UIStaff uiStaff) {
                    switch (uiStaff.getPositionId()) {
                        case 1:
                            injector.getEventBus().fireEvent(new OperatorLoginEvent(uiStaff));
                            break;
                        case 2:
                            injector.getEventBus().fireEvent(new ManagerLoginEvent(uiStaff));
                            break;
                        case 3:
                            injector.getEventBus().fireEvent(new EngineerLoginEvent(uiStaff));
                            break;
                        default:
                            injector.getEventBus().fireEvent(new LoginAllowEvent(uiStaff));
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void doPasswordReset() {
        String login = this.display.getLogin().getText();
        if (!login.equals("")) {
            try {
                UserService.App.getInstance().changePassword(login, new AsyncCallback<Void>() {
                    @Override
                    public void onFailure(Throwable throwable) {
                        Dialog.getInstance().createDialogBoxWithMessage("Error", throwable.getMessage());
                    }

                    @Override
                    public void onSuccess(Void aVoid) {
                        Dialog.getInstance().createDialogBoxWithMessage("Success", "Your new password was sent to your email");
                    }
                });
            } catch (Exception e) {
                Dialog.getInstance().createDialogBoxWithMessage("Error", e.getMessage());
            }
        } else {
            Dialog.getInstance().createDialogBoxWithMessage("Error", "Please fill your username first");
        }
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

    public interface Display {
        HasClickHandlers getLoginButton();

        HasClickHandlers getPasswordResetButton();

        HasText getLogin();

        HasText getPassword();

        Widget asWidget();
    }
}
